package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.*;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IStrategyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class StrategyServiceImpl implements IStrategyService {

    @Autowired
    private StrategyMapper strategyMapper;
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private CommentTagMapper commentTagMapper;
    @Autowired
    private OfCommentMapper ofCommentMapper;

    @Override
    public PageInfo getPageInfo(StrategyQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Strategy> list = strategyMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(Strategy strategy) {
        if (strategy.getId() == null) {
            strategyMapper.insert(strategy);
        } else {
            strategyMapper.updateByPrimaryKey(strategy);
        }
    }

    @Override
    public List<Strategy> list() {
        return strategyMapper.selectAll();
    }

    @Override
    public PageInfo listByState(StrategyQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Strategy> list = strategyMapper.listByState(qo);
        return new PageInfo(list);
    }

    @Override
    public Strategy get(Long strategyId) {
        return strategyMapper.selectByPrimaryKey(strategyId);
    }

    @Override
    public void deleteStrategyTravel(Long id) {
        strategyMapper.deleteStrategyTravel(id);
    }

    @Override
    public void saveStrategyTravel(Long strategyId, Long travelId) {
        Travel travel = travelMapper.selectById(strategyId, travelId);
        if (travel != null) {
            throw new RuntimeException("已经存在该攻略游记了,不需重复添加!");
        }
        Integer count = travelMapper.selectCollectCountForTravel(travelId);
        strategyMapper.saveStrategyTravel(strategyId, travelId, count);
    }

    @Override
    public PageInfo getTagsByStrategyId(Long strategyId, QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Map<String, Object>> list = strategyMapper.selectTagsByStrategyId(strategyId);
        return new PageInfo(list);

    }

    @Override
    public void saveStrategyComments(Comment comment, String myTags, Long[] ids) {

        //保存评论
        comment.setDate(new Date());
        commentMapper.insert(comment);
        //保存标签
        //查询出所有的标签名称
        List<String> allTagName = tagMapper.selectAllTagName();
        String[] tagNames = myTags.trim().split("\\s+");
        CommentTag commentTag = new CommentTag();
        commentTag.setComment_id(comment.getId());
        if (tagNames != null && !tagNames.equals("")) {
            for (String temp : tagNames) {
                if (!allTagName.contains(temp)) {
                    //如果数据库中不存在重名的标签,就保存
                    Tag tag = new Tag();
                    tag.setName(temp);
                    tagMapper.insert(tag);
                    Long id = tag.getId();
                    //将评论id和标签id的关系存入数据库
                    commentTag.setTag_id(id);
                    commentTagMapper.insert(commentTag);

                }
            }
        }


        //将评论id和标签id的关系存入数据库
        if (ids!=null) {
            for (Long id : ids) {
                commentTag.setTag_id(id);
                commentTagMapper.insert(commentTag);
            }
        }
    }


    /**
     * 根据攻略id获取攻略的所有评论
     * @param strategyId 攻略id
     * @param qo
     * @return
     */
    @Override
    public PageInfo getStrategyCommentsByStrategyId(Long strategyId, QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Comment> list = commentMapper.selectStrategyCommentsByStrategyId(strategyId);
        return new PageInfo(list);

    }

    @Override
    public PageInfo getStrategyCommentByStrategyCommentId(Long strategyCommentId, QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Comment> list = commentMapper.selectStrategyCommentByStrategyCommentId(strategyCommentId);
        return new PageInfo(list);

    }

    @Override
    public void saveStrategyOfComments(OfComment ofComment) {
        ofComment.setDate(new Date());
        ofCommentMapper.insert(ofComment);
    }

    /**
     * 获取某条母评论的所有自评论
     * @param strategyCommentId 母评论的id
     * @param qo
     * @return
     */
    @Override
    public PageInfo getStrategyOfCommentByStrategyCommentId(Long strategyCommentId, QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<OfComment> list = ofCommentMapper.selectByStrategyCommentId(strategyCommentId);
        for (OfComment ofComment : list) {
            //设置这条子评论的点赞数
            ofComment.setOfOfCommentLikers(ofCommentMapper.selectOfOfCommentLikers(ofComment.getId()));
            //设置这条子评论的评论数
            ofComment.setOfOfCommentNums(ofCommentMapper.selectOfOfCommentNums(ofComment.getId()));
        }
        return new PageInfo(list);

    }

    @Override
    public Integer showStrategyCommentLikersByStrategyCommentId(Long strategyCommentId, Long likerId) {
        //判断用户是否已点过赞
        OfComment temp = ofCommentMapper.selectOfCommentByCommentIdAndLikerId(strategyCommentId, likerId);
        if (temp != null) {
            ofCommentMapper.deleteByPrimaryKey(temp.getId());
            //返回某条母评论的点赞数
            return ofCommentMapper.selectCommentLikersCount(strategyCommentId);
        }

        //否则插入一条对母评论的点赞记录
        OfComment ofComment = new OfComment();
        //设置点赞时间
        ofComment.setDate(new Date());
        //设置点赞人
        User liker = new User();
        liker.setId(likerId);
        ofComment.setLiker(liker);
        //设置要点赞的母评论
        Comment comment = new Comment();
        comment.setId(strategyCommentId);
        /*ofComment.setComment(comment);*/
        //插入点赞记录
        ofCommentMapper.insert(ofComment);
        //再从数据库中查询某条母评论最新的点赞数
        return ofCommentMapper.selectCommentLikersCount(strategyCommentId);
    }

    @Override
    public Integer showStrategyOfCommentLikersByStrategyCommentId(Long strategyOfCommentId, Long likerId) {
        //判断用户是否已对该自评论点赞
        OfComment temp = ofCommentMapper.selectByOfCommentIdAndLikerId(strategyOfCommentId,likerId);
        if (temp!=null) {
            //点过赞就从数据库中删除这条记录,取消点赞
            ofCommentMapper.deleteByPrimaryKey(temp.getId());
            return ofCommentMapper.selectOfOfCommentLikersByParentId(strategyOfCommentId);
        }
        //否则插入一条对自评论的点赞记录
        OfComment ofComment = new OfComment();
        //设置点赞时间
        ofComment.setDate(new Date());
        //设置parent_id
        ofComment.setParentId(strategyOfCommentId);
        //设置点赞人
        User liker = new User();
        liker.setId(likerId);
        ofComment.setLiker(liker);
        ofCommentMapper.insert(ofComment);
        return ofCommentMapper.selectOfOfCommentLikersByParentId(strategyOfCommentId);

    }

    @Override
    public void saveStrategyOfOfComments(OfComment ofComment) {
        ofComment.setDate(new Date());
        ofCommentMapper.insert(ofComment);
    }
}
